//
//  StatusCell.swift
//  WeiBo_One
//
//  Created by apple on 15/10/27.
//  Copyright © 2015年 蜡笔小炎. All rights reserved.
//

import UIKit
import SnapKit

// 微博 Cell 控件间距
let statusCellMargin: CGFloat = 12
// 微博 Cell 头像大小
let statusIconWidth: CGFloat = 35

// 微博 cell
class StatusCell: UITableViewCell {
    
    // MARK: - 视图控件
    // 微博视图模型，数据入口
    var viewModel: StatusViewModel? {
        didSet {
            //给顶部视图数据
            topView.statusViewModel = viewModel!
            //给配图视图数据
            pictureView.statusViewModel = viewModel!
            //设置微博文字
            contentLabel.text = viewModel?.status.text
            // 测试配图高度
            let margin = viewModel?.thumvnailUrls?.count == 0 ? 0 : statusCellMargin
            pictureView.snp_updateConstraints { (make) -> Void in
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
                make.height.equalTo(pictureView.bounds.height)
                make.width.equalTo(pictureView.bounds.width)
            }
        }
    }
    
    // 顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    // 微博文字
    private lazy var contentLabel: UILabel = UILabel(title: "微博文字",
        fontSize: 15,
        color: UIColor.darkGrayColor(),
        screenInset: statusCellMargin)
    // 底部视图
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    // 配图视图
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    
    // 构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 设置界面
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}

  
    // 计算行高
    func rowHeight(vm: StatusViewModel) -> CGFloat {
        //设置数据模型
        viewModel = vm
        //更新视图
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
}

// 设置界面
extension StatusCell {
    
    private func setupUI() {
        
        // cell 背景颜色
        backgroundColor = UIColor.orangeColor()
        
        // Label 设置换行宽度
        contentLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * statusCellMargin
        
        // 添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 添加约束
        autoSet()
    }
    
    // 自动布局
    private func autoSet() {
        //顶部视图
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.height.equalTo(statusCellMargin + statusIconWidth)
            make.right.equalTo(contentView.snp_right)
        }
        //文字label
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(statusCellMargin)
        }
        // 3> 配图视图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(statusCellMargin)
            make.width.equalTo(300)
            make.height.equalTo(90)
        }
        // 4> 底部视图

        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
        }
    }
}